//
//  XHMyDownViewController.m
//  testApp
//
//  Created by ColdMountain on 2016/12/6.
//  Copyright © 2016年 ColdMountain. All rights reserved.
//

#import "XHMyDownViewController.h"

#import "Services.h"
#import "XHPlayerCell.h"

#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface XHMyDownViewController ()<XHPlayerCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong,nonatomic) NSString * filePath;
@end

@implementation XHMyDownViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHPlayerCell" bundle:nil] forCellReuseIdentifier:XHPlayerCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    self.filePath = [userDefaults objectForKey:@"path"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
    if([ServiceManager fileExistsAtPath:self.filePath])
    {
        rows=1;
    }
    else
    {
        rows=0;
    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHPlayerCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHPlayerCellIdentifier];
    cell.delegate=self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=70;
    return height;
}

#pragma - mark XHPlayerCellDelegate

- (void)runAction:(UIButton *)button
{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSString * path = [userDefaults objectForKey:@"path"];
    NSURL *url = [NSURL fileURLWithPath:path];
    AVPlayerViewController *viewController = [[AVPlayerViewController alloc]init];
    viewController.player=[[AVPlayer alloc]initWithURL:url];
    [viewController.player play];
    NSLog(@"本地播放");
    [self presentViewController:viewController animated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
